//
//  SLUpdateFirmView.m
//  SlockApp
//
//  Created by 凯朱 on 2024/7/30.
//

#import "SLUpdateFirmView.h"
#import "YQCacheManager.h"
@implementation SLUpdateFirmView
-(void)awakeFromNib{
    [super awakeFromNib];
    _topView.layer.cornerRadius = 20;
    _topView.layer.masksToBounds = YES;
    
    _bgView.layer.cornerRadius = 20;
    _bgView.layer.masksToBounds = YES;
    
    _downloadBtn.btnStyle = 1;
    [_downloadBtn setTitle:kLocalString(@"Download") forState:UIControlStateNormal];
    
    _sureBtn.btnStyle = 1;
    [_sureBtn setTitle:kLocalString(@"OK") forState:UIControlStateNormal];
    _sureBtn.hidden = YES;
    
    _topLbl1.text = kLocalString(@"Current version");
    _tipLbl1.text = kLocalString(@"Update version");
    _tipLbl3.text = kLocalString(@"What's new");
//    _infoLbl.text = kLocalString(@"This update includes improvements and bug fixes for your HexForce Padlock G1.");
    _tipNewLbl.text = kLocalString(@"Your padlock have already updeted to the lasest version.");
}

-(void)refresSubViewWith:(NSDictionary *)updateInfo{
    if (updateInfo) {
        _tipBgView.hidden = YES;
        _topContentLbl.text = updateInfo[@"curVersion"];
        _tipContentLbl1.text = updateInfo[@"serverVersion"];
        _tipLbl2.text = updateInfo[@"firmwareName"];
        _infoLbl.text = kSTRING(updateInfo[@"dscription"]).length > 0 ? kSTRING(updateInfo[@"dscription"]) : @"--";
        CGSize size = STRING_SIZE_FONT(kScreenWidth-80, kSTRING(updateInfo[@"dscription"]), kFont(14));
        _bgViewHeight.constant = 400+MAX(size.height, 50);
        CGFloat fileSize = [kSTRING(updateInfo[@"firmwareSize"]) floatValue];
        NSString *sizeStr = @"";
        if (fileSize >= 1048576) {//1048576bt = 1M  小于1m的显示KB 大于1m显示M
            sizeStr = [NSString stringWithFormat:@"%.2fM",fileSize/1024/1024];
        }else{
            sizeStr = [NSString stringWithFormat:@"%.1fKB",fileSize/1024];
        }
        self->_sizeLbl.text = sizeStr;
        if ([kCurClickDevice.deviceCategory isEqualToString:@"2"]) {
            if (kSTRING(updateInfo[@"url"]).length > 0) {
                [_downloadBtn setTitle:kLocalString(@"Start install") forState:UIControlStateNormal];
            }
        }else{
            _filePath = [[YQCacheManager shareManager] getDownloadDataFromCacheWithRequestUrl:updateInfo[@"url"]];
            if (_filePath && _filePath.absoluteString.length > 0) {
                [_downloadBtn setTitle:kLocalString(@"Start install") forState:UIControlStateNormal];
            }
        }
    }else{
        _tipBgView.hidden = NO;
        _bgView.hidden = YES;
        [self bringSubviewToFront:_tipBgView];
    }
}

-(void)updateWithFinish:(NSString *)version{
    _tipBgView.hidden = NO;
    _bgView.hidden = YES;
    _topContentLbl.text = version;
    [self bringSubviewToFront:_tipBgView];
}

- (IBAction)btnAction:(id)sender {
    if (sender == _downloadBtn) {
        if (self.btnBlock) self.btnBlock(1);
    }else if (sender == _sureBtn){
        if (self.btnBlock) self.btnBlock(2);
    }
}


@end
